<?php
/**
 *
 *  
 * Date: 2024/1/16
 * Email: < >
 */


namespace app\traits;

use app\api\model\ChickenCards as ChickenCards;
use app\api\model\Wallets as Wallets;

trait OpChickenCard
{
    public function randomPushCards($count)
    {
        //判断是否有库存
        $total_cards = ChickenCards::where('id', '2')->value('total_cards');
        if (intval($total_cards) < $count) {
            return "库存不够请重新选择数量";
        }
        //抽每张卡
        $cards_list = [];
        for ($i = 0; $i < $count; $i++) {
            $selectedCard = $this->drawCard();
            $cards_list[] = $selectedCard;
        }
        return $cards_list;
    }
    //获取账户卡片余额
    public function cardsYue($uid)
    {
        $s1 = Wallets::where('wallet_user_id',$uid)->value('green_card_1');
        $s2 = Wallets::where('wallet_user_id',$uid)->value('green_card_2');
        $s3 = Wallets::where('wallet_user_id',$uid)->value('green_card_3');
        $s4 = Wallets::where('wallet_user_id',$uid)->value('green_card_4');
        $s5 = Wallets::where('wallet_user_id',$uid)->value('green_card_5');
        $s6 = Wallets::where('wallet_user_id',$uid)->value('blue_card_1');
        $s7 = Wallets::where('wallet_user_id',$uid)->value('blue_card_2');
        $s8 = Wallets::where('wallet_user_id',$uid)->value('gold_card_1');
        $s9 = Wallets::where('wallet_user_id',$uid)->value('red_card_1');
        $s10 = Wallets::where('wallet_user_id',$uid)->value('black_card_1');
        $res = $s1+$s2+$s3+$s4+$s5+$s6+$s7+$s8+$s9+$s10;
        return $res;
    }

    function drawCard()
    {
        //抽卡
        $cards_base = ChickenCards::where('id', '2')->select('green_card_1_draw_rate', 'green_card_2_draw_rate',
            'green_card_3_draw_rate', 'green_card_4_draw_rate', 'green_card_5_draw_rate', 'blue_card_1_draw_rate',
            'blue_card_2_draw_rate', 'gold_card_1_draw_rate', 'red_card_1_draw_rate', 'black_card_1_draw_rate')->first();
        $cards = [
            'green_card_1' => floatval($cards_base->green_card_1_draw_rate),
            'green_card_2' => floatval($cards_base->green_card_2_draw_rate),
            'green_card_3' => floatval($cards_base->green_card_3_draw_rate),
            'green_card_4' => floatval($cards_base->green_card_4_draw_rate),
            'green_card_5' => floatval($cards_base->green_card_5_draw_rate),
            'blue_card_1' => floatval($cards_base->blue_card_1_draw_rate),
            'blue_card_2' => floatval($cards_base->blue_card_2_draw_rate),
            'gold_card_1' => floatval($cards_base->gold_card_1_draw_rate),
            'red_card_1' => floatval($cards_base->red_card_1_draw_rate),
            'black_card_1' => floatval($cards_base->black_card_1_draw_rate)
        ];
        $totalCards = array_sum($cards);
        $randomNumber = rand(1, $totalCards);
        foreach ($cards as $color => $quantity) {
            if ($randomNumber <= $quantity) {
                return $color;
            }
            $randomNumber -= $quantity;
        }
    }

    function setCardsList($cards_list)
    {
        $res = [];
        foreach ($cards_list as $card) {
            if (isset($res[$card])) {
                $res[$card]++;
            } else {
                $res[$card] = 1;
            }
        }
        return $res;
    }

    function generateId($phone_number)
    {
        $offset = rand(0, 10);
        $id = strtoupper(substr(md5($phone_number), $offset, 11));
        return $id;
    }

    function album_type_card($val){
        $card =[
            'blue_card_1'=>1,
            'blue_card_2'=>1,
            'green_card_1'=>1,
            'green_card_2'=>1,
            'green_card_3'=>1,
            'green_card_4'=>1,
            'green_card_5'=>1
        ];
        switch($val){
            case "gold_card_album":
                $card['gold_card_1'] = 1;
                break;
            case "red_card_album":
                $card['gold_card_1'] = 1;
                $card['red_card_1'] = 1;
                break;
            case "black_card_album":
                $card['gold_card_1'] = 1;
                $card['red_card_1'] = 1;
                $card['black_card_1'] = 1;
                break;
            default:
                break;
        }
        return $card;
    }

    function album_get_eggs($val){
        switch($val) {
            case "gold_card_album":
                return 'gold_card_eggs';
                break;
            case "red_card_album":
                return 'red_card_eggs';
                break;
            case "black_card_album":
                return 'black_card_eggs';
                break;
            default:
                break;
        }
    }

    function verifyTradePwd($tradePwd,$tradePwdDB){
        if(password_verify($tradePwd,$tradePwdDB)){
            return true;
        }else{
            return false;
        }
    }
}